package com.itany.mybatis.tm.impl;

import com.itany.mybatis.tm.TransactionManager;
import com.itany.mybatis.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * Description: MyBatis的事务管理器
 * Author: liudaolin@itany.com
 * Date: 2021/07/20 13:32
 * Version: 1.0 版本信息
 */
public class MyBatisTransactionManagerImpl implements TransactionManager {
    @Override
    public void begin() {
        // 获取session时, 默认开启事务
//        MyBatisUtil.getSession();
    }

    @Override
    public void commit() {
        try {
            SqlSession session = MyBatisUtil.getSession();
            session.commit();
        } finally {
            // 确保最终会关闭 SqlSession
            MyBatisUtil.close();
        }
    }

    @Override
    public void rollback() {
        try {
            SqlSession session = MyBatisUtil.getSession();
            session.rollback();
        } finally {
            // 确保最终会关闭 SqlSession
            MyBatisUtil.close();
        }
    }
}
